package com.hh.tax.service.impl;/*
 * @Auther:ZhengXueCheng
 * @Date:2023/8/2
 * @VERSON:1.8
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hh.commen.PageVO;
import com.hh.tax.bean.Tax;
import com.hh.tax.mapper.TaxMapper;
import com.hh.tax.service.TaxService;
import com.hh.tax.vo.AddTaxVo;
import com.hh.tax.vo.StatusChangeVo;
import com.hh.tax.vo.TaxVo;
import com.hh.tax.vo.UpdateTaxVo;
import com.hh.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TaxServiceImpl implements TaxService {
    @Autowired
    private TaxMapper taxMapper;
    @Override
    public ResultVo findAllTaxByPage(TaxVo taxVo) {
        PageHelper.startPage((taxVo.getPageIndex()), taxVo.getPageSize());
        List<Tax> taxList = taxMapper.selectAllPlanCategory(taxVo);
        PageInfo<Tax> pageInfo = new PageInfo<>(taxList);
        PageVO pageVO = new PageVO();
        pageVO.setData(taxList);
        pageVO.setTotalNumber(pageInfo.getTotal());
        pageVO.setTotalPage(pageInfo.getPageNum());
        return ResultVo.success(pageVO);
    }

    @Override
    public ResultVo addTax(AddTaxVo addTaxVo) {
        if (taxMapper.judgeTaxCodeIsExist(addTaxVo.getTaxCode()) > 0) {
            return ResultVo.error("税率编码重复，请重新输入");
        }
        Tax tax = new Tax();
        tax.setTax(Double.parseDouble(addTaxVo.getTax()));
        tax.setTaxCode(addTaxVo.getTaxCode());
        tax.setTaxDec(addTaxVo.getTaxDec());
        if (addTaxVo.getStatus()) {
            tax.setStatus(1);
        }else {
            tax.setStatus(0);

        }
        Date date = new Date();
        tax.setCreateTime(date);
        tax.setUpdateTime(date);
        taxMapper.insert(tax);
        return ResultVo.success();
    }

    @Override
    public ResultVo updateStatus(StatusChangeVo statusChangeVo) {
        taxMapper.updateStatusById(statusChangeVo);
        return ResultVo.success();
    }

    @Override
    public ResultVo updateTax(UpdateTaxVo updateTaxVo) {
        if (taxMapper.judgeUpdateTaxCodeIsExist(updateTaxVo.getId(), updateTaxVo.getTaxCode()) > 0) {
            return ResultVo.error("税率编码重复，请重新输入");
        }

        Tax tax = new Tax();
        tax.setId(updateTaxVo.getId());
        tax.setTax(Double.parseDouble(updateTaxVo.getTax()));
        tax.setTaxCode(updateTaxVo.getTaxCode());
        tax.setTaxDec(updateTaxVo.getTaxDec());
        tax.setStatus(updateTaxVo.getStatus());
        Date date = new Date();
        tax.setUpdateTime(date);
        tax.setVersion(updateTaxVo.getVersion());
        taxMapper.updateByPrimaryKeySelective(tax);
        return ResultVo.success();
    }

}
